//
//  ShareSDKDM.m
//  ShareSDKDM
//
//  Created by apple on 2018/3/6.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ShareSDKDM.h"

@implementation ShareSDKDM
//初始化第三方平台
+ (void)share_RegisterActivePatformsCompeletion:(void(^)(NSError * error))handler
{
    [ShareSDK registerActivePlatforms:@[@(SSDKPlatformTypeWechat),
                                        @(SSDKPlatformTypeQQ),
                                        @(SSDKPlatformTypeSinaWeibo),
                                        ]
                             onImport:^(SSDKPlatformType platformType)
     {
         switch (platformType)
         {
             case SSDKPlatformTypeWechat:
                 [ShareSDKConnector connectWeChat:[WXApi class]];
                 break;
             case SSDKPlatformTypeQQ:
                 [ShareSDKConnector connectQQ:[QQApiInterface class] tencentOAuthClass:[TencentOAuth class]];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 [ShareSDKConnector connectWeibo:[WeiboSDK class]];
                 break;
             default:
                 break;
         }
     }onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo)
     {
         
         switch (platformType)
         {
                 
             case SSDKPlatformTypeWechat:
                 [appInfo SSDKSetupWeChatByAppId:@"wx21e211be92212a74"
                                       appSecret:@"ecd364e0897e198e64db7adb205a942b"];
                 break;
             case SSDKPlatformTypeQQ: //完成
                 [appInfo SSDKSetupQQByAppId:@"1106689579"
                                      appKey:@"mrVsjdWzxGMw9xF0"
                                    authType:SSDKAuthTypeBoth];
                 break;
             case SSDKPlatformTypeSinaWeibo:
                 //设置新浪微博应用信息,其中authType设置为使用SSO＋Web形式授权
                 [appInfo SSDKSetupSinaWeiboByAppKey:@"568898243"
                                           appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                                         redirectUri:@"http://www.sharesdk.cn"
                                            authType:SSDKAuthTypeBoth];
                 break;
             default:
                 break;
         }
         if(handler)
             handler(nil);
     }];
}
//第三方登录
+ (void)share_loginPlatform:(SSDKPlatformType)platform fetchUserInfo:(void(^)(SSDKUser *user))compeletion result:(void(^)(NSError *error))result
{
    [SSEThirdPartyLoginHelper loginByPlatform:platform
                                   onUserSync:^(SSDKUser *user, SSEUserAssociateHandler associateHandler) {
                                       
                                       //在此回调中可以将社交平台用户信息与自身用户系统进行绑定，最后使用一个唯一用户标识来关联此用户信息。
                                       //在此示例中没有跟用户系统关联，则使用一个社交用户对应一个系统用户的方式。将社交用户的uid作为关联ID传入associateHandler。
                                       associateHandler (user.uid, user, user);
                                       if(compeletion)
                                           compeletion(user);
                                       
                                   }
                                onLoginResult:^(SSDKResponseState state, SSEBaseUser *user, NSError *error) {
                                    
                                    if (state == SSDKResponseStateSuccess){
                                        if(result)
                                            result(nil);
                                    }else{
                                        if(result)
                                            result(error);
                                    }
                                    
                                    
                                }];
}

//分享到第三方平台
+ (void)share_sharePlatform:(SSDKPlatformType)platform title:(NSString *)title images:(NSArray *)images  content:(NSString *)content url:(NSString *)url compeletion:(void(^)(SSDKResponseState state,NSError *error))compeletion
{
    //1、创建分享参数
     NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    if(title.length == 0){
        title = @"分享标题";
    }
    
    if(content.length == 0){
        content = @"分享内容";
    }
    if(url.length == 0){
        url = @"www.baidu.com";
    }
    [shareParams SSDKSetupShareParamsByText:content
                                     images:images
                                        url:[NSURL URLWithString:url]
                                      title:title
                                       type:SSDKContentTypeAuto];
    
    //2.开始分享
    [ShareSDK share:platform
         parameters:shareParams
     onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) {
         switch (state) {
             case SSDKResponseStateSuccess:{
                 if(compeletion)
                     compeletion(SSDKResponseStateSuccess,nil);
                 break;
             }
             case SSDKResponseStateFail:{
                 if(compeletion)
                     compeletion(SSDKResponseStateFail,error);
                 break;
             }
             case SSDKResponseStateCancel:{
                 if(compeletion)
                     compeletion(SSDKResponseStateCancel,nil);
                 break;
             }
             default:
                 break;
         }
     }];
    
}

+ (BOOL)checkInstallWX
{
    BOOL success = [WXApi isWXAppInstalled];
    return success;
}

+ (BOOL)checkInstallQQ
{
    BOOL success = [QQApiInterface isQQInstalled];
    return success;
}

@end
